package com.yaoweigroup.community.util;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;

import android.content.Context;
import android.content.res.AssetManager;

public class Tools {

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static double round(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	public static void writeAssestFile(Context context, String data) {
		AssetManager assetManager = context.getAssets();
		InputStream input = null;
		try {
			input = assetManager.open("data.txt");
			InputStreamReader inputReader = new InputStreamReader(input,
					"UTF-8");
			BufferedReader bReader = new BufferedReader(inputReader);
			FileWriter fw = new FileWriter(data);
			String line = "";
			while ((line = bReader.readLine()) != null) {
				fw.write(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
